package models

import (
	"go-admin-official/modules/database"
)

type ThemesVersionModel struct {
	Base `json:"-"`

	Id        int64  `json:"id"`
	ThemeId   int64  `json:"theme_id"`
	UserId    int64  `json:"user_id"`
	Version   string `json:"version"`
	Content   string `json:"content"`
	Readme    string `json:"readme"`
	Url       string `json:"url"`
	CreatedAt string `json:"created_at"`
	UpdatedAt string `json:"updated_at"`
}

func ThemesVersion() ThemesVersionModel {
	return ThemesVersionModel{Base: Base{Table: "themes_version"}}
}

func (t ThemesVersionModel) WithTx(tx *database.SqlTx) ThemesVersionModel {
	t.Tx = tx
	return t
}

func (t ThemesVersionModel) Find(id interface{}) ThemesVersionModel {
	item, _ := database.Table(t.Table).Find(id)

	if item == nil {
		return ThemesVersion()
	}

	return t.MapToModel(item)
}

func (t ThemesVersionModel) Update(key string, value interface{}) (int64, error) {
	return database.Table(t.Table).
		Where("id", "=", t.Id).
		Update(database.H{
			key: value,
		})
}

func (t ThemesVersionModel) IsEmpty() bool {
	return t.Id == int64(0)
}

func (t ThemesVersionModel) IsNotEmpty() bool {
	return t.Id != int64(0)
}

func (t ThemesVersionModel) MapToModel(m map[string]interface{}) ThemesVersionModel {
	t.Id, _ = m["id"].(int64)
	t.ThemeId, _ = m["theme_id"].(int64)
	t.UserId, _ = m["user_id"].(int64)
	t.Version, _ = m["version"].(string)
	t.Content, _ = m["content"].(string)
	t.Readme, _ = m["readme"].(string)
	t.Url, _ = m["url"].(string)
	t.CreatedAt, _ = m["created_at"].(string)
	t.UpdatedAt, _ = m["updated_at"].(string)
	return t
}
